package com.jqsoft.launcher.activity.highblood;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.jqsoft.launcher.activity.WebViewActivity;
import com.jqsoft.launcher.activity.bean.HBPGuanLi_PersonInfo;
import com.jqsoft.launcher.activity.bean.UserLoginInfo;
import com.jqsoft.launcher.activity.gairui.NibpExample;
import com.jqsoft.launcher.activity.gairui.Spo2Example;
import com.jqsoft.launcher.common.BphsConstants;
import com.jqsoft.launcher.common.BphsURL;
import com.jqsoft.launcher.util.CommonUtil;
import com.jqsoft.launcher.util.DateTools;
import com.jqsoft.launcher.util.FileService;
import com.jqsoft.launcher.util.Log;
import com.jqsoft.launcher.util.MessageTools;
import com.jqsoft.launcher.util.StringTools;
import com.jqsoft.launcher.util.WebServiceUtils;
import com.jqsoft.launcher.util.XmlUtil;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class HBPFollowUpRecordActivity3 extends WebViewActivity implements
		View.OnClickListener {

	HBPGuanLi_PersonInfo personInfo;
	String jsonstr = null;
	UserLoginInfo info = UserLoginInfo.getInstances();
	private int updateIndex = -1;
	private String sPersonID1;
	private String sNo1;// 档案编号
	private String sname1;// 姓名
	private String sCardNo1;// 证件号码
	private String ishuixian = "0";
	private String xueya = "0";
	private String xinlv = "0";
	private JSONArray jsonObj2 = null;
	private String sAreaCode, sAreaName;

	@Override
	protected void initGui() {
		super.initGui();
		// webView.loadUrl("file:///android_asset/jktj.html");
		webView.loadUrl("file:///android_asset/hbp_followup_record.html");// 高血压
		webView.addJavascriptInterface(new JSObject(), "contact");
	}

	@Override
	protected void initAction() {
		setGoBack();
	}

	@Override
	protected void initData() {
		setTitle("高血压患者随访服务记录表");
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		if (bundle != null) {
			personInfo = (HBPGuanLi_PersonInfo) bundle
					.get("HBPGuanLi_PersonInfo");
			jsonstr = bundle.getString("Tjdata");
			sPersonID1 = bundle.getString("sPersonID");
			sNo1 = bundle.getString("sNo");
			sname1 = bundle.getString("sname");
			sCardNo1 = bundle.getString("sCardNo");
			updateIndex = bundle.getInt("chakan");

			// 上次回显
			if (StringTools.isEmpty(jsonstr)) {
				jsonstr = bundle.getString("shangciData");
				if (StringTools.isNotEmpty(jsonstr)) {
					ishuixian = "1";
				}
			}
		}
	}

	@Override
	public void onClick(View v) {
	}

	private final class JSObject {
		// 初始化页面
		@JavascriptInterface
		public void getHtml() {
			runOnUiThread(new Runnable() {
				@SuppressWarnings("unused")
				@Override
				public void run() {
					try {
						Map<String, String> map = new HashMap<String, String>();
						map.put("HypertensionVisiting_VisitingType",
								"VisitingTypes"); // 随访方式
						map.put("HypertensionVisiting_Symptom", "Symptoms");// 症状
						map.put("HypertensionVisiting_HaveSolt", "HaveSolts");// 目前摄盐情况、下次目标摄取盐情况
						map.put("HypertensionVisiting_PsychplogicalRecover",
								"PsychplogicalRecovers");// 心理调整、遵医行为
						map.put("PsychosisFollowUp_sDrugCompliance",
								"DrugCompliances");// 药物服从性
						map.put("HypertensionVisiting_DrugReaction",
								"DrugReactions");// 药物不良反应
						map.put("HypertensionVisiting_VisitingClassify",
								"VisitingClassifys");// 此次随访分类
						JSONObject jsonObj = CommonUtil.getAllDictionary(map);
						if (personInfo != null) {
							// jsonObj.put("sName",
							// personInfo.getsPersonName()); //页面回显姓名

							jsonObj.put("sName", personInfo.getsPersonName()); // 页面回显姓名
							jsonObj.put("sNo", personInfo.getsNo()); // 页面回显档案号
						}
						jsonObj.put("dVisitingDate", DateTools
								.getFormatStringByDF_YYYY_MM_DD(new Date()));// 页面回显随访日期
						jsonObj.put("dNextVisitingDate", DateTools
								.getFormatStringByDF_YYYY_MM_DD(DateTools
										.getNextMonthByDate(new Date(), 3)));// 下次随访日期
						jsonObj.put("sNextVisitingPlace",
								info.getsOrganizationName());// 随访单位
						jsonObj.put("DoctorList", info.getJsonArrayDoctors());// 回显随访医生列表
						jsonObj.put("sVisitingDoctor", info.getsUserName());// 随访医生
						JSONArray jsonObj2 = null;
						if (jsonstr != null) {
							jsonstr = jsonstr.replace("\\n", "");
							jsonObj2 = new JSONArray(jsonstr);

							for (int i = 0; i < jsonObj2.length() - 1; i++) {
								JSONObject o = (JSONObject) jsonObj2.get(i);
								sAreaCode = o.getString("sAreaCode");
								sAreaName = o.getString("sAreaName");

							}
						}
						// 初始化
						String json = jsonObj.toString();
						webView.loadUrl("javascript:showHtml('" + json + "','"
								+ jsonObj2 + "','" + ishuixian + "')");
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}

		// 保存操作
		@JavascriptInterface
		public void save(String uploadType, String sCompianceName,
				String sVisitingClassifyName, String sDrugReactionName,
				String sDrugComplianceName, String sPsychplogicalRecoveryName,
				String sHaveSoltName2, String sHaveSoltName,
				String sVisitingTypeName, String sSymptomName,
				String txtdHeight, String sName, String sNo,
				String dVisitingDate, String sVisitingTypeCode,
				String sSymptomCode, String sSymptomOther,
				String iSystolicPressure, String iDiastolicPressure,
				String nWeight1, String nWeight2, String nBodyMassIndex1,
				String nBodyMassIndex2, String nHeartRate, String sOther,
				String nDaySmoke1, String nDaySmoke2, String nDayDrink1,
				String nDayDrink2, String nSport_EveryWeek1,
				String nSport_EveryMinute1, String nSport_EveryWeek2,
				String nSport_EveryMinute2, String sHaveSoltCode,
				String sHaveSoltCode2, String sPsychplogicalRecoveryCode,
				String sCompianceCode, String sAccessoryExamination,
				String sDrugComplianceCode, String sDrugReactionCode,
				String sDrugReactionOther, String sVisitingClassifyCode,
				String sTransferTreatment, String sTransferTreatmentDept,
				String dNextVisitingDate, String sNextVisitingPlace,
				String sVisitingDoctor, String iWeatherHealthEducation,
				String sWeatherHealthEducationContent,
				String BaoJianZhidaoContent, String BaoJianZhidaoContentCode,
				String sDrugName1, String iDrug_EveryDay1,
				String sDrug_EveryTime1, String sDrugName2,
				String iDrug_EveryDay2, String sDrug_EveryTime2,
				String sDrugName3, String iDrug_EveryDay3,
				String sDrug_EveryTime3, String sDrugName4,
				String iDrug_EveryDay4, String sDrug_EveryTime4,
				String sDrugQtdw1, String sDrugQtdw2, String sDrugQtdw3,
				String sDrugQtdw4, String sDrug_qitadw1, String sDrug_qitadw2,
				String sDrug_qitadw3, String sDrug_qitadw4) {
			try {
				String gHypertensionVisitingKey = CommonUtil.getUUID();
				final JSONArray jsonArrayData = new JSONArray();
				JSONObject jsonData = new JSONObject();
				JSONObject jsonVistingInfo = new JSONObject();
				jsonVistingInfo.put("xueya", xueya);
				jsonVistingInfo.put("xinlv", xinlv);
				// jsonVistingInfo.put("xuetang", s);
				// jsonVistingInfo.put("xuehongdanbai", s);
				// jsonVistingInfo.put("tiwen", s);
				// jsonVistingInfo.put("mailv", s);
				// jsonVistingInfo.put("huxipinlv", s);
				// jsonVistingInfo.put("xindian", s);

				jsonVistingInfo.put("gKey", gHypertensionVisitingKey);
				jsonVistingInfo.put("sName", sName);
				if (personInfo != null) {
					jsonVistingInfo.put("sPersonID", personInfo.getsPersonID());
					jsonVistingInfo.put("sNo", personInfo.getsNo());
				} else {
					jsonVistingInfo.put("sPersonID", sPersonID1);
					jsonVistingInfo.put("sNo", sNo1);
				}
				jsonVistingInfo.put("nHeight", txtdHeight);// ?
				jsonVistingInfo.put("dVisitingYear",
						dVisitingDate.substring(0, 4));
				jsonVistingInfo.put("dVisitingDate", dVisitingDate);
				jsonVistingInfo.put("sVisitingTypeName", sVisitingTypeName);
				jsonVistingInfo.put("sVisitingTypeCode", sVisitingTypeCode);
				jsonVistingInfo.put("sSymptomName", sSymptomName);
				jsonVistingInfo.put("sSymptomCode", sSymptomCode);
				jsonVistingInfo.put("sSymptomOther", sSymptomOther);
				jsonVistingInfo.put("iSystolicPressure", iSystolicPressure);
				jsonVistingInfo.put("iDiastolicPressure", iDiastolicPressure);
				jsonVistingInfo.put("nWeight1", nWeight1);
				jsonVistingInfo.put("nWeight2", nWeight2);
				jsonVistingInfo.put("nBodyMassIndex1", nBodyMassIndex1);
				jsonVistingInfo.put("nBodyMassIndex2", nBodyMassIndex2);
				jsonVistingInfo.put("nHeartRate", nHeartRate);
				jsonVistingInfo.put("sOther", sOther);
				jsonVistingInfo.put("nDaySmoke1", nDaySmoke1);
				jsonVistingInfo.put("nDaySmoke2", nDaySmoke2);
				jsonVistingInfo.put("nDayDrink1", nDayDrink1);
				jsonVistingInfo.put("nDayDrink2", nDayDrink2);
				jsonVistingInfo.put("nSport_EveryWeek1", nSport_EveryWeek1);
				jsonVistingInfo.put("nSport_EveryWeek2", nSport_EveryWeek2);
				jsonVistingInfo.put("nSport_EveryMinute1", nSport_EveryMinute1);
				jsonVistingInfo.put("nSport_EveryMinute2", nSport_EveryMinute2);
				jsonVistingInfo.put("sHaveSoltName", sHaveSoltName);
				jsonVistingInfo.put("sHaveSoltCode", sHaveSoltCode);
				jsonVistingInfo.put("sHaveSoltName2", sHaveSoltName2);
				jsonVistingInfo.put("sHaveSoltCode2", sHaveSoltCode2);
				jsonVistingInfo.put("sPsychplogicalRecoveryName",
						sPsychplogicalRecoveryName);
				jsonVistingInfo.put("sPsychplogicalRecoveryCode",
						sPsychplogicalRecoveryCode);
				jsonVistingInfo.put("sCompianceName", sCompianceName);
				jsonVistingInfo.put("sCompianceCode", sCompianceCode);
				jsonVistingInfo.put("sAccessoryExamination",
						sAccessoryExamination);
				jsonVistingInfo.put("sDrugComplianceName", sDrugComplianceName);
				jsonVistingInfo.put("sDrugComplianceCode", sDrugComplianceCode);
				jsonVistingInfo.put("sDrugReactionName", sDrugReactionName);
				jsonVistingInfo.put("sDrugReactionCode", sDrugReactionCode);
				jsonVistingInfo.put("sDrugReactionOther", sDrugReactionOther);
				jsonVistingInfo.put("sVisitingClassifyName",
						sVisitingClassifyName);
				jsonVistingInfo.put("sVisitingClassifyCode",
						sVisitingClassifyCode);
				jsonVistingInfo.put("sTransferTreatment", sTransferTreatment);
				jsonVistingInfo.put("sTransferTreatmentDept",
						sTransferTreatmentDept);
				jsonVistingInfo.put("dNextVisitingDate", dNextVisitingDate);
				jsonVistingInfo.put("sVisitingDoctor", sVisitingDoctor);
				jsonVistingInfo.put("sAddUser", info.getsLoginName());
				jsonVistingInfo.put("dAddTime", DateTools
						.getFormatStringByDF_YYYY_MM_DD_HH_MM_SS(new Date()));
				jsonVistingInfo.put("sUpdateUser", "");
				jsonVistingInfo.put("dUpdateTime", "");
				jsonVistingInfo.put("sDeleteUser", "");
				jsonVistingInfo.put("dDeleteTime", "");
				jsonVistingInfo.put("sInputDeptName",
						info.getsOrganizationName());
				jsonVistingInfo.put("sInputDeptCode",
						info.getsOrganizationKey());
				jsonVistingInfo.put("iState", 1);
				jsonVistingInfo.put("sAreaCode", sAreaCode);// info.getsManagementDivisionCode()
				jsonVistingInfo.put("sAreaName", sAreaName);// info.getsManagementDivisionName()
				jsonVistingInfo.put("iWeatherHealthEducation",
						iWeatherHealthEducation);
				jsonVistingInfo.put("sWeatherHealthEducationContent",
						sWeatherHealthEducationContent);
				jsonVistingInfo.put("BaoJianZhidaoContent",
						BaoJianZhidaoContent);
				jsonVistingInfo.put("BaoJianZhidaoContentCode",
						BaoJianZhidaoContentCode);
				jsonVistingInfo.put("sVisitingDoctorCode", "");
				jsonVistingInfo.put("sVisitingPlaceCode", "");
				jsonVistingInfo.put("sNextVisitingPlace", sNextVisitingPlace);
				jsonArrayData.put(jsonVistingInfo);
				JSONArray jsonArray = new JSONArray();
				if (StringTools.isNotEmpty(sDrugName1)) {
					JSONObject jsonObj = new JSONObject();
					jsonObj.put("gKey", CommonUtil.getUUID());
					jsonObj.put("gHypertensionVisitingKey",
							gHypertensionVisitingKey);
					if (personInfo != null) {
						jsonObj.put("sPersonID", personInfo.getsPersonID());
					} else {
						jsonObj.put("sPersonID", sPersonID1);
					}
					jsonObj.put("sDrugName", sDrugName1);
					jsonObj.put("iDrug_EveryDay", iDrug_EveryDay1);
					jsonObj.put("sDrug_EveryTime", sDrug_EveryTime1);
					jsonObj.put("sAddUser", info.getsLoginName());
					jsonObj.put(
							"dAddTime",
							DateTools
									.getFormatStringByDF_YYYY_MM_DD_HH_MM_SS(new Date()));
					jsonObj.put("sUpdateUser", "");
					jsonObj.put("dUpdateTime", "");
					jsonObj.put("sDeleteUser", "");
					jsonObj.put("dDeleteTime", "");

					jsonObj.put("sDrugQtdw", sDrugQtdw1);
					jsonObj.put("sDrug_qitadw", sDrug_qitadw1);
					jsonObj.put("sInputDeptName", info.getsOrganizationName());
					jsonObj.put("sInputDeptCode", info.getsOrganizationKey());
					jsonObj.put("iState", 1);
					jsonObj.put("sort", 1);
					jsonArray.put(jsonObj);
				}
				if (StringTools.isNotEmpty(sDrugName2)) {
					JSONObject jsonObj = new JSONObject();
					jsonObj.put("gKey", CommonUtil.getUUID());
					jsonObj.put("gHypertensionVisitingKey",
							gHypertensionVisitingKey);
					if (personInfo != null) {
						jsonObj.put("sPersonID", personInfo.getsPersonID());
					} else {
						jsonObj.put("sPersonID", sPersonID1);
					}
					jsonObj.put("sDrugName", sDrugName2);
					jsonObj.put("iDrug_EveryDay", iDrug_EveryDay2);
					jsonObj.put("sDrug_EveryTime", sDrug_EveryTime2);
					jsonObj.put("sAddUser", info.getsLoginName());
					jsonObj.put(
							"dAddTime",
							DateTools
									.getFormatStringByDF_YYYY_MM_DD_HH_MM_SS(new Date()));
					jsonObj.put("sUpdateUser", "");
					jsonObj.put("dUpdateTime", "");
					jsonObj.put("sDeleteUser", "");
					jsonObj.put("dDeleteTime", "");
					jsonObj.put("sDrugQtdw", sDrugQtdw2);
					jsonObj.put("sDrug_qitadw", sDrug_qitadw2);
					jsonObj.put("sInputDeptName", info.getsOrganizationName());
					jsonObj.put("sInputDeptCode", info.getsOrganizationKey());
					jsonObj.put("iState", 1);
					jsonObj.put("sort", "2");
					jsonArray.put(jsonObj);
				}
				if (StringTools.isNotEmpty(sDrugName3)) {
					JSONObject jsonObj = new JSONObject();
					jsonObj.put("gKey", CommonUtil.getUUID());
					jsonObj.put("gHypertensionVisitingKey",
							gHypertensionVisitingKey);
					if (personInfo != null) {
						jsonObj.put("sPersonID", personInfo.getsPersonID());
					} else {
						jsonObj.put("sPersonID", sPersonID1);
					}
					jsonObj.put("sDrugName", sDrugName3);
					jsonObj.put("iDrug_EveryDay", iDrug_EveryDay3);
					jsonObj.put("sDrug_EveryTime", sDrug_EveryTime3);
					jsonObj.put("sAddUser", info.getsLoginName());
					jsonObj.put(
							"dAddTime",
							DateTools
									.getFormatStringByDF_YYYY_MM_DD_HH_MM_SS(new Date()));
					jsonObj.put("sUpdateUser", "");
					jsonObj.put("dUpdateTime", "");
					jsonObj.put("sDeleteUser", "");
					jsonObj.put("dDeleteTime", "");
					jsonObj.put("sDrugQtdw", sDrugQtdw3);
					jsonObj.put("sDrug_qitadw", sDrug_qitadw3);
					jsonObj.put("sInputDeptName", info.getsOrganizationName());
					jsonObj.put("sInputDeptCode", info.getsOrganizationKey());
					jsonObj.put("iState", 1);
					jsonObj.put("sort", "3");
					jsonArray.put(jsonObj);
				}
				if (StringTools.isNotEmpty(sDrugName4)) {
					JSONObject jsonObj = new JSONObject();
					jsonObj.put("gKey", CommonUtil.getUUID());
					jsonObj.put("gHypertensionVisitingKey",
							gHypertensionVisitingKey);
					if (personInfo != null) {
						jsonObj.put("sPersonID", personInfo.getsPersonID());
					} else {
						jsonObj.put("sPersonID", sPersonID1);
					}
					jsonObj.put("sDrugName", sDrugName4);
					jsonObj.put("iDrug_EveryDay", iDrug_EveryDay4);
					jsonObj.put("sDrug_EveryTime", sDrug_EveryTime4);
					jsonObj.put("sAddUser", info.getsLoginName());
					jsonObj.put(
							"dAddTime",
							DateTools
									.getFormatStringByDF_YYYY_MM_DD_HH_MM_SS(new Date()));
					jsonObj.put("sUpdateUser", "");
					jsonObj.put("dUpdateTime", "");
					jsonObj.put("sDeleteUser", "");
					jsonObj.put("dDeleteTime", "");
					jsonObj.put("sDrugQtdw", sDrugQtdw4);
					jsonObj.put("sDrug_qitadw", sDrug_qitadw4);
					jsonObj.put("sInputDeptName", info.getsOrganizationName());
					jsonObj.put("sInputDeptCode", info.getsOrganizationKey());
					jsonObj.put("iState", 1);
					jsonObj.put("sort", "4");
					jsonArray.put(jsonObj);
				}
				jsonData.put("sMedicalList", jsonArray);
				jsonArrayData.put(jsonData);
				Log.i("benny", jsonArrayData.toString());
				// [{"nDaySmoke1":"","nDaySmoke2":"","sHaveSoltCode2":"","sWeatherHealthEducationContent":"","sAccessoryExamination":"","sDrugReactionOther":"","sDrugReactionCode":"","sAreaCode":"340000","sNextVisitingPlace":"安徽省卫生厅__ddd","dVisitingDate":"2014-06-23 10:48:56","sTransferTreatmentDept":"","sDrugComplianceCode":"","dDeleteTime":"","sDrugComplianceName":"","sCompianceCode":"","BaoJianZhidaoContent":"","iWeatherHealthEducation":"0","sDrugReactionName":"","sVisitingClassifyCode":"","sVisitingTypeName":"","sInputDeptCode":"afc31406-730c-45f7-834b-cb5acc7f8fe2","sVisitingTypeCode":"","sUpdateUser":"","sPsychplogicalRecoveryName":"","sVisitingClassifyName":"","sHaveSoltCode":"","dUpdateTime":"","iState":1,"sDeleteUser":"","sInputDeptName":"安徽省卫生厅__ddd","BaoJianZhidaoContentCode":"","sOther":"","dAddTime":"2014-06-23 10:49:28","dNextVisitingDate":"2014-09-23","sVisitingDoctor":"安徽省卫生厅","sSymptomCode":"","sPsychplogicalRecoveryCode":"","sAddUser":"admin","sTransferTreatment":"","nSport_EveryMinute2":"","gKey":"ed563280-186c-4b2a-8590-3dd42068d971","nSport_EveryWeek1":"","nSport_EveryWeek2":"","nSport_EveryMinute1":"","dVisitingYear":"2014","sNo":"34082400300100210","sHaveSoltName":"","sVisitingDoctorCode":"","sHaveSoltName2":"","sSymptomName":"","sAreaName":"安徽省","sVisitingPlaceCode":"","sCompianceName":"","nDayDrink2":"","sSymptomOther":"","nDayDrink1":"","sPersonID":"123"},{"sMedicalList":[]}]
				if ("2".equals(uploadType)) {
					HBPFollowUpRecordActivity3.this
							.runOnUiThread(new Runnable() {
								@Override
								public void run() {
									try {
										boolean issuccess = false;
										if (updateIndex >= 0
												&& personInfo == null) {

											issuccess = new FileService(act)
													.updateFromRom(
															updateIndex,
															sPersonID1,
															sNo1,
															sname1,
															sCardNo1,
															"高血压随访",
															jsonArrayData
																	.toString(),
															"append.txt");
										} else {
											issuccess = new FileService(act).saveToRom(
													personInfo.getsPersonID(),
													personInfo.getsNo(),
													personInfo.getsPersonName(),
													personInfo.getsCardNo(),
													"高血压随访",
													jsonArrayData.toString(),
													"append.txt");
										}
										if (issuccess) {
											MessageTools
													.showDialogOk(ctx,
															"保存成功！本次随访数据保存在本机，请到“离线设置”-“上传随访信息”中上传或者修改数据！");
										} else {
											Toast.makeText(
													HBPFollowUpRecordActivity3.this,
													"保存失败！", Toast.LENGTH_SHORT)
													.show();
										}
									} catch (Exception e) {
										Toast.makeText(
												HBPFollowUpRecordActivity3.this,
												"保存失败！", Toast.LENGTH_SHORT)
												.show();
										e.printStackTrace();
									}
								}
							});
				} else if (BphsConstants.isNetAvailable
						&& "1".equals(uploadType)) {
					HBPFollowUpRecordActivity3.this
							.runOnUiThread(new Runnable() {
								@Override
								public void run() {

									// 创建了对话框的构造器
									Builder builder = new Builder(
											HBPFollowUpRecordActivity3.this);
									builder.setTitle("温馨提示");
									builder.setMessage("确定将本次随访数据提交至服务器吗？注意：提交后本条数据无法在平板上再次修改！");
									builder.setPositiveButton("确定",
											new OnClickListener() {

												public void onClick(
														DialogInterface dialog,
														int which) {
													try {
														String methodName = BphsURL
																.getCommonPath("AddHypertensionVisiting_METHOD");
														String serviceURL = BphsURL
																.getCommonPath("GAddHypertensionVisiting_URL");
														// String methodName =
														// "AddHypertensionVisiting";
														// String serviceURL =
														// "http://60.171.146.122:9098/WebService/Andrews/HypertensionWebService.asmx";
														String result = WebServiceUtils
																.requestByParams(
																		methodName,
																		jsonArrayData
																				.toString(),
																		serviceURL);
														if (StringUtils
																.isNotBlank(result)) {
															JSONObject json = new JSONObject(
																	result);
															String sResult = json
																	.getString("sSuccess");
															if (sResult
																	.equals("1")) {
																Toast.makeText(
																		HBPFollowUpRecordActivity3.this,
																		"保存完成！",
																		Toast.LENGTH_SHORT)
																		.show();
															} else {
																Toast.makeText(
																		HBPFollowUpRecordActivity3.this,
																		"保存失败！",
																		Toast.LENGTH_SHORT)
																		.show();
															}
														} else {
															Toast.makeText(
																	HBPFollowUpRecordActivity3.this,
																	"远程服务器故障！",
																	Toast.LENGTH_SHORT)
																	.show();
														}
													} catch (Exception e) {
														e.printStackTrace();
													}

												}
											});
									builder.setNegativeButton("取消",
											new OnClickListener() {

												public void onClick(
														DialogInterface dialog,
														int which) {
													// Toast.makeText(HBPFollowUpRecordActivity.this,
													// "保存失败！",
													// Toast.LENGTH_SHORT).show();
												}
											});
									AlertDialog dialog = builder.create();
									dialog.setCancelable(false);
									dialog.setCanceledOnTouchOutside(false);
									dialog.show();

								}

							});
				} else {
					Toast.makeText(HBPFollowUpRecordActivity3.this,
							"离线状态只能保存随访信息！", Toast.LENGTH_LONG).show();
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		// 调用蓝牙接口返回xml数据
		@JavascriptInterface
		public void getXML(String type) {
			switch (BphsConstants.deviceBranchType) {
			case 0:
				Intent intent = new Intent();
				// 通讯接口
				intent.setComponent(new ComponentName(
						"com.example.uray.bluetooth.comm.app",
						"com.example.uray.bluetooth.comm.app.CommActivity"));
				// 参数配置接口
				// intent.setComponent(new
				// ComponentName("com.example.uray.bluetooth.comm.app",
				// "com.example.uray.bluetooth.comm.app.ConfigActivity"));
				intent.setAction(Intent.ACTION_VIEW);
				if (type.equals("1")) {
					startActivityForResult(intent, 1); // 获取血压值
				} else {
					startActivityForResult(intent, 2); // 获取心率值
				}
				break;
			case 1:
				if ("1".equals(type)) {
					Intent iNibp = new Intent(HBPFollowUpRecordActivity3.this,
							NibpExample.class);
					startActivityForResult(iNibp, 3); // 获取血压值
					// startActivity(iNibp);
				} else if ("2".equals(type)) {
					Intent iSpo2 = new Intent(HBPFollowUpRecordActivity3.this,
							Spo2Example.class);
					startActivityForResult(iSpo2, 4); // 获取心率值
				}
				break;
			default:
				break;
			}
		}
	}

	// 解析蓝牙接口返回的XML格式的字符串
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1 && resultCode == RESULT_OK) {
			// 获取血压值
			String sb = data.getStringExtra("return");
			// StringBuffer sb = new StringBuffer();
			// try {
			// InputStream in = getResources().getAssets().open("c.xml");
			// byte[] b = new byte[4096];
			// int n;
			// while ((n = in.read(b))!= -1){
			// sb.append(new String(b,0,n));
			// }
			// } catch (IOException e) {
			// e.printStackTrace();
			// }
			String jsonStr = XmlUtil.parseXml(sb);
			// MessageTools.showDialogOk(ctx, "返回结果", sb);
			// Toast.makeText(ctx, a, 1).show();
			xueya = "1";
			webView.loadUrl("javascript:showXML('" + jsonStr + "')");
		}
		if (requestCode == 2 && resultCode == RESULT_OK) {
			xinlv = "1";
			// 获取心率值
			String sb = data.getStringExtra("return");

			final String jsonStr = XmlUtil.parseXml(sb);
			try {
				JSONObject json = new JSONObject(jsonStr);
				final String cHeartRate = json.getString("cHeartRate") == "null" ? ""
						: json.getString("cHeartRate");
				final String cPulseRate = json.getString("cPulseRate") == "null" ? ""
						: json.getString("cPulseRate");
				if ("-100".equals(cHeartRate) && !"-100".equals(cPulseRate)) {
					Builder builder = new Builder(
							HBPFollowUpRecordActivity3.this);
					builder.setTitle("温馨提示");
					builder.setMessage("心率准确数据您可以用心电设备测出，夹手指血氧仪只能测脉率。但心率约等于脉率，是否将脉率设置为心率？");
					builder.setPositiveButton("确定", new OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							try {
								// jsonStr.replace("\"cHeartRate\":\"-100\"",
								// "\"cHeartRate\":\""+cPulseRate+"\"");
								webView.loadUrl("javascript:showXML2('"
										+ cPulseRate + "')");
							} catch (Exception e) {
								e.printStackTrace();
							}

						}
					});
					builder.setNegativeButton("取消", new OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							webView.loadUrl("javascript:showXML2('"
									+ cHeartRate + "')");
						}
					});
					AlertDialog dialog = builder.create();
					dialog.setCancelable(false);
					dialog.setCanceledOnTouchOutside(false);
					dialog.show();

				} else {
					webView.loadUrl("javascript:showXML2('" + cHeartRate + "')");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// MessageTools.showDialogOk(ctx, "返回结果", sb);
			// Toast.makeText(ctx, a, 1).show();

		}
		if (requestCode == 3 && data != null) {
			xueya = "1";
			// 获取血压值
			String jsonstr = data.getStringExtra("jsonstr");
			webView.loadUrl("javascript:showXML('" + jsonstr + "')");

		}
		if (requestCode == 4 && data != null) {
			xinlv = "1";
			final String cPulseRate = data.getStringExtra("cPulseRate");
			Builder builder = new Builder(
					HBPFollowUpRecordActivity3.this);
			builder.setTitle("温馨提示");
			builder.setMessage("心率准确数据您可以用心电设备测出，夹手指血氧仪只能测脉率。但心率约等于脉率，是否将脉率设置为心率？");
			builder.setPositiveButton("确定", new OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					try {
						// jsonStr.replace("\"cHeartRate\":\"-100\"",
						// "\"cHeartRate\":\""+cPulseRate+"\"");
						webView.loadUrl("javascript:showXML2('" + cPulseRate
								+ "')");
					} catch (Exception e) {
						e.printStackTrace();
					}

				}
			});
			builder.setNegativeButton("取消", new OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {

				}
			});
			AlertDialog dialog = builder.create();
			dialog.setCancelable(false);
			dialog.setCanceledOnTouchOutside(false);
			dialog.show();
		}
	}
}
